﻿//This filter is tested and seems to work correctly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Rotates the Channels of the bitmap for each pixel
    /// This should also be interesting for other color spaces
    /// </summary>
    public class RotateChannels : BaseImageFilter
    {
        protected delegate Color RotateChannelsDelegate(Color _color);
        protected RotateChannelsDelegate rotateChannelsDelegate = null;

        /// <summary>
        /// Default constructor
        /// </summary>
        public RotateChannels()
        {
            m_AbstractColorSpace = new LowLevelGraphics.ColorSpaces.RGB(Color.Transparent);
            rotateChannelsDelegate = new RotateChannelsDelegate(ColorFunctions.RotateChannels);
            if (m_AbstractColorSpace.GetType() != typeof(LowLevelGraphics.ColorSpaces.RGB))
            {
                rotateChannelsDelegate = new RotateChannelsDelegate(RotateChannelsColorSpace);
            }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int y = 0;
            int x = 0;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    color = rotateChannelsDelegate(color);
                    bitmap.SetPixel(x, y, color);
                }
            }

            return _bitmap;
        }

        /// <summary>
        /// Rotate Channels Color space
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        private Color RotateChannelsColorSpace(Color _color)
        {
            object[] aObject = m_AbstractColorSpace.GetValues();
            object oTemp = aObject[0];
            aObject[0] = aObject[1];
            aObject[1] = aObject[2];
            aObject[2] = oTemp;
            m_AbstractColorSpace.SetValues(aObject);
            return m_AbstractColorSpace.ToColor();
        }

        /// <summary>
        /// Clones this channel
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new RotateChannels();
        }
    }
}
